package com.visionblue.business.ship.page.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.enums.WasteTypeEnum
import com.visionblue.business.ship.model.enums.getUnitText
import com.visionblue.business.ship.model.repository.db.CollectionRecord
import com.visionblue.business.ship.util.FormatUtils
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.dialog_collection_confirm.*
import kotlinx.android.synthetic.main.item_collection_confirm_dialog.*
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * @author: xlh
 * @date: 2020-06-04 5:39 PM
 * @desc:
 */
class CollectionConfirmDialog(
    context: Context,
    shipName: String?,
    collectionRecords: List<CollectionRecord>,
    isShowPrintOption: Boolean,
    isOutput: Boolean?
) :
    Dialog(context, R.style.blurry_slide_dialog) {

    var onDialogButtonClickListener: OnDialogButtonClickListener? = null

    init {
        setContentView(R.layout.dialog_collection_confirm)

        rvPollution.layoutManager = LinearLayoutManager(context)
        rvPollution.adapter = PollutionAdapter(collectionRecords)

        tvShipNamePrefix.text = if (isOutput == true) "转出对象：" else "接收对象："

        tvShipName.text = shipName ?: ""

        tvTitleView.text = if (isOutput == true) "转出确认单" else "接收确认单"
        checkbox.isChecked = isOutput == false

        tvBack.setOnClickListener {
            dismiss()
        }

        checkbox.visibility = if (isShowPrintOption) View.VISIBLE else View.GONE

        tvSure.setOnClickListener {
            onDialogButtonClickListener?.onSureClick(
                isShowPrintOption && checkbox.isChecked,
                collectionRecords
            )
            dismiss()
        }
    }

    class PollutionAdapter(private val collectionRecords: List<CollectionRecord>) :
        RecyclerView.Adapter<PollutionAdapter.VH>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
            val itemView = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_collection_confirm_dialog, parent, false)
            return VH(itemView)
        }

        override fun getItemCount() = collectionRecords.size

        override fun onBindViewHolder(holder: VH, position: Int) {
            holder.bindData(position, collectionRecords[position])
        }

        class VH(override val containerView: View) : RecyclerView.ViewHolder(containerView),
            LayoutContainer {

            fun bindData(position: Int, collectionRecord: CollectionRecord) {
                val wasteTypeEnum = WasteTypeEnum.getWasteType(collectionRecord.wasteType)
                tvNo.text = "${position + 1}."
                tvType.text = wasteTypeEnum?.desc
                val showWeight = collectionRecord.getShowWeight()
         /*       val valueText = if (wasteTypeEnum?.isSolid() == true) {
                    BigDecimal(showWeight.toString()).divide(BigDecimal(1000))
                        .setScale(3, RoundingMode.DOWN).toPlainString()
                } else {
                    FormatUtils.format3Decimal(showWeight?.toDouble())
                }*/
                val valueText = FormatUtils.format3Decimal(showWeight?.toDouble())
                tvWeight.text =
                    "$valueText ${wasteTypeEnum?.getUnitText(showWeight.toString())}"

            }
        }
    }

    interface OnDialogButtonClickListener {
        fun onSureClick(
            isPrint: Boolean,
            collectionRecords: List<CollectionRecord>
        )
    }

}